package
{	
	import assets.*;
	
	import com.greensock.TweenMax;
	import com.greensock.easing.EaseLookup;
	import com.greensock.easing.Linear;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.filters.BlurFilter;
	import flash.ui.Keyboard;
	
	import gameobjects.Enemy;
	
	import monitors.*;
	
	import states.*;
	
	[SWF(width="800", height="570", frameRate="40", backgroundColor="#333333")]	
	
	public class OneManTD extends Sprite
	{		
		private var game:Game = new Game(paused);
		private var menu:Menu = new Menu();
		
		/** 
		 * Konstrukotor
		 */
		public function OneManTD()
		{
			// da bi igra bila svesna stage-a
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private var gamePaused:Boolean = false;
		
		private function paused(i:int):void
		{
			TweenMax.to(game, .2, { blurFilter: {blurX: 20, blurY: 20}, ease:Linear.easeNone });
			gamePaused = true;
			Globals.state = Globals.MENU_STATE;
			addChild(menu);
			menu.show(i);
		}
		
		private var blur:BlurFilter = new BlurFilter(12, 12, 10);
		
		/**
		 * Inicijalizuje igru
		 */
		private function init(event:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			
			Globals.previousDate = new Date();
			Globals.stage = stage;
			
			TweenMax.pauseAll();
			game.filters = [blur];
			addChild(game);
			addChild(menu);
			menu.show(1);
		}
		
		/**
		 * enterFrameHandler, poziva se pri svakom iscrtavanju frejma
		 */
		private function enterFrameHandler(event:Event):void
		{
			Globals.currentDate = new Date();
			Globals.dt = Globals.currentDate.time - Globals.previousDate.time;
			
			switch(Globals.state)
			{
				case Globals.INTRO_STATE:
				{
					//TODO: INTRO STATE enter frame
				}
				case Globals.MENU_STATE:
				{
					// TODO: MENU STATE enter frame
					break;
				}
				case Globals.GAME_STATE:
				{
					game.enterFrameHanler(event);
					break;
				}
			}
			
			Globals.previousDate = Globals.currentDate;
		}
		
		/**
		 * Key up event handler
		 */
		private function keyUpHandler(event:KeyboardEvent):void
		{
			//KeyMonitor.keyReleased(event.keyCode);
			switch(Globals.state)
			{
				case Globals.INTRO_STATE:
				{
					//TODO: INTRO STATE enter frame
					if (event.keyCode == Keyboard.ENTER)
					{
						//TODO: Change state
					}
				}
				case Globals.MENU_STATE:
				{
					// TODO: MENU STATE enter frame
					if (event.keyCode == Keyboard.ENTER)
					{
						if (gamePaused)
							game.resume();
						else
							game.start();
						menu.hide();
						removeChild(menu);
						TweenMax.to(game, .2, { blurFilter: {blurX: 0, blurY: 0}, ease:Linear.easeNone });
						Globals.state = Globals.GAME_STATE;
					}
					break;
				}
				case Globals.GAME_STATE:
				{
					game.keyUpHandler(event);
					break;
				}
			}
		}
		
		/**
		 * Key down event handler
		 */
		private function keyDownHandler(event:KeyboardEvent):void 
		{
			//KeyMonitor.keyPressed(event.keyCode);
		}
	}
}